package cn.com.idmy.auth.context;

import cn.com.idmy.auth.exception.AuthException;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;

@Accessors(fluent = true)
public class Contexts {
    @Setter
    private volatile static Context context;
    @Setter
    private volatile static SecondContext secondContext;

    @NotNull
    protected static Context contextOrSecond() {
        if (context != null) {
            if (secondContext == null || context.isValid()) {
                return context;
            }
        }

        if (secondContext != null && secondContext.isValid()) {
            return secondContext;
        }

        throw new AuthException("上下文为空");
    }

    @NotNull
    public static Context context() {
        return contextOrSecond();
    }

    public static long accountType() {
        return contextOrSecond().accountType();
    }

    @NotNull
    public static Request request() {
        return contextOrSecond().request();
    }

    @NotNull
    public static Response response() {
        return contextOrSecond().response();
    }

    @NotNull
    public static Storage storage() {
        return contextOrSecond().storage();
    }
}
